<?php
require_once ("../dao/CardDetailDAO.php");
require_once ("../dao/BankDAO.php");
require_once ("../dao/CardTypeDAO.php");
require_once ("../dao/CardLevelDAO.php");
require_once('RedirectForward.php');

$url = "admin/admin.php?content=quanlythetindung";

if(isset($_POST['acc'])) {
	$act = $_POST['acc'];
	if($act == "add") {
		if(isset($_POST['bank']) && isset($_POST['selectcardtype']) && isset($_POST['selectcardlevel']) && isset($_POST['form_blancestranfer']) && isset($_POST['form_monthstranfer']) && isset($_POST['form_rate']) && isset($_POST['form_annualfee']) && isset($_POST['form_freeday']) && isset($_POST['form_latepayment']) && isset($_POST['form_minirepayment']) && isset($_POST['form_phiruttient']) && isset($_POST['form_khuvuccapthe']) && isset($_POST['form_linkgotosite'])&& isset($_POST['form_hanmucthe'])) {
			
			$result = false;
			$id = time();
			$idbank = $_POST['bank'];
			$arbank = getBank($idbank);
			if(count($arbank) > 0) {
				$bankname = $arbank[0][1];
				$banklogo = $arbank[0][2];
				
				$hanmuc = $_POST['form_hanmucthe'];
				$btranfer = $_POST['form_blancestranfer'];
				$mtranfer = $_POST['form_monthstranfer'];
				$rate = $_POST['form_rate'];
				$annualfee = $_POST['form_annualfee'];
				$cardid = $_POST['selectcardtype'];
				
				$arcardtype = getCardType($cardid);
				if(count($arcardtype) > 0) {
					$ctname = $arcardtype[0][1];
					$ctlogo = $arcardtype[0][2];
					
					$freeday = $_POST['form_freeday'];
					$paymentfee = $_POST['form_latepayment'];
					$minpayment = $_POST['form_minirepayment'];
					
					$clid = $_POST['selectcardlevel'];
					
					$arcLevel = getCardLevel($clid);
					if(count($arcLevel) > 0) {
						$clname = $arcLevel[0][1];
						
						$phiruttien = $_POST['form_phiruttient'];
						$areacard = $_POST['form_khuvuccapthe'];
						$linkgosite = $_POST['form_linkgotosite'];
						
						if(insertCardDetail($id, $idbank, $bankname, $banklogo, $btranfer, $mtranfer, $rate, $annualfee, $cardid, $ctname, $ctlogo, $freeday, $paymentfee, $minpayment, $clid, $clname, $phiruttien, $areacard, $linkgosite, $hanmuc)) {
							$result = true;
						}
					}
				}
			}
			
			$url .= "&as=add-laisuat-the";
			if($result) {
				$url .= "&&mess=Thêm thông tin thành công.";	
			} else {
				$url .= "&&mess=Thêm thông tin thất bại.";
			}
		}
	} else if($act == "edit") {
		if(isset($_POST['idcad']) && isset($_POST['bank']) && isset($_POST['selectcardtype']) && isset($_POST['selectcardlevel']) && isset($_POST['form_blancestranfer']) && isset($_POST['form_monthstranfer']) && isset($_POST['form_rate']) && isset($_POST['form_annualfee']) && isset($_POST['form_freeday']) && isset($_POST['form_latepayment']) && isset($_POST['form_minirepayment']) && isset($_POST['form_phiruttient']) && isset($_POST['form_khuvuccapthe']) && isset($_POST['form_linkgotosite']) && isset($_POST['form_hanmucthe'])) {
			
			$result = false;
			$id = $_POST['idcad'];
			$idbank = $_POST['bank'];
			$arbank = getBank($idbank);
			if(count($arbank) > 0) {
				$bankname = $arbank[0][1];
				$banklogo = $arbank[0][2];
				
				$hanmuc = $_POST['form_hanmucthe'];
				$btranfer = $_POST['form_blancestranfer'];
				$mtranfer = $_POST['form_monthstranfer'];
				$rate = $_POST['form_rate'];
				$annualfee = $_POST['form_annualfee'];
				$cardid = $_POST['selectcardtype'];
				
				$arcardtype = getCardType($cardid);
				if(count($arcardtype) > 0) {
					$ctname = $arcardtype[0][1];
					$ctlogo = $arcardtype[0][2];
					
					$freeday = $_POST['form_freeday'];
					$paymentfee = $_POST['form_latepayment'];
					$minpayment = $_POST['form_minirepayment'];
					
					$clid = $_POST['selectcardlevel'];
					
					$arcLevel = getCardLevel($clid);
					if(count($arcLevel) > 0) {
						$clname = $arcLevel[0][1];
						
						$phiruttien = $_POST['form_phiruttient'];
						$areacard = $_POST['form_khuvuccapthe'];
						$linkgosite = $_POST['form_linkgotosite'];
						
						if(updateCardDetail($id, $idbank, $bankname, $banklogo, $btranfer, $mtranfer, $rate, $annualfee, $cardid, $ctname, $ctlogo, $freeday, $paymentfee, $minpayment, $clid, $clname, $phiruttien, $areacard, $linkgosite, $hanmuc)) {
							$result = true;
						}
					}
				}
			}
			
			$url .= "&as=edit-laisuat-the&id=".$id;
			if($result) {
				$url .= "&mess=Cập nhật thông tin thành công.";	
			} else {
				$url .= "&mess=Cập nhật thông tin thất bại.";
			}
		}
	}
}

redirect($url);
?>